package com.ruoyi.life.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.life.domain.FeMeterBill;
import com.ruoyi.life.domain.vo.FeBillVo;
import com.ruoyi.life.service.IFeBillService;
import com.ruoyi.life.service.IFeMeterBillService;
import com.ruoyi.omp.api.domain.FeBill;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 应收账单Controller
 *
 * @author cjie
 * @date 2021-09-23
 */
@Api(tags = " 应收账单")
@RestController
@RequestMapping("/feBill")
public class FeBillController extends BaseController {
    @Autowired
    private IFeBillService feBillService;

    @Autowired
    private IFeMeterBillService feMeterBillService;

    /**
     * 查询应收账单列表
     */
    @ApiOperation(value="查询应收账单列表", notes="查询应收账单列表")
    @PreAuthorize(hasPermi = "life:feBill:list")
    @GetMapping("/list")
    public TableDataInfo list(FeBill feBill) {
        startPage();
        List<FeBill> list = feBillService.selectFeBillList(feBill);
        return getDataTable(list);
    }

    /**
     * 查询应收账单列表 (后台调用)
     */
    @PostMapping("/listBill")
    public R<List<FeBill>> listBill(@RequestBody FeBill feBill) {
        List<FeBill> list = feBillService.listBill(feBill);
        return R.ok(list);
    }

    /**
     * 查询对应记录收费类型的最后一条账单信息 (后台调用)
     */
    @PostMapping("/lastBill")
    public R<FeBill> lastBill(@RequestBody FeBill feBill) {
        FeBill bill = feBillService.selectLastBillByRecordIdAndFeeType(feBill);
        return R.ok(bill);
    }

    /**
     * 导出应收账单列表
     */
    @ApiOperation(value="导出应收账单列表", notes="导出应收账单列表")
    @PreAuthorize(hasPermi = "life:feBill:export")
    @Log(title = "应收账单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeBill feBill) throws IOException {
        List<FeBill> list = feBillService.selectFeBillList(feBill);
        ExcelUtil<FeBill> util = new ExcelUtil<FeBill>(FeBill.class);
        util.exportExcel(response, list, "应收账单数据");
    }
    /**
     * 获取应收账单详细信息
     */
    @ApiOperation(value="获取应收账单详细信息", notes="获取应收账单详细信息")
    @PreAuthorize(hasPermi = "life:feBill:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        FeBill feBill=feBillService.selectFeBillById(id);
        if (feBill.getFeeTypeName().equals("水电费")){
            FeBillVo feBillVo =new FeBillVo();
            FeMeterBill feMeterBill=new FeMeterBill();
            BeanUtils.copyBeanProp(feBillVo,feBill);
            feMeterBill.setBillNum(feBill.getBillNum());
            feMeterBill.setMeterType("水表");
            feBillVo.setWaterMeterBills(feMeterBillService.selectFeMeterBillList(feMeterBill));
            feMeterBill.setMeterType("电表");
            feBillVo.setElMeterBills(feMeterBillService.selectFeMeterBillList(feMeterBill));
            return AjaxResult.success(feBillVo);
        }
        return AjaxResult.success(feBill);
    }

    /**
     * 新增应收账单
     */
    @ApiOperation(value="新增应收账单", notes="新增应收账单")
    @PreAuthorize(hasPermi = "life:feBill:add")
    @Log(title = "应收账单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FeBill feBill) {
        return toAjax(feBillService.insertFeBill(feBill));
    }

    /**
     * 新增应收账单 (后台调用)
     */
    @PostMapping("/add")
    public R addBill(@RequestBody FeBill feBill) {
        return toR(feBillService.insertFeBill(feBill));
    }

    /**
     * 修改应收账单
     */
    @ApiOperation(value="修改应收账单", notes="修改应收账单")
    @PreAuthorize(hasPermi = "life:feBill:edit")
    @Log(title = "应收账单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FeBill feBill) {
        return toAjax(feBillService.updateFeBill(feBill));
    }

    /**
     * 删除应收账单
     */
    @ApiOperation(value="删除应收账单", notes="删除应收账单")
    @ApiImplicitParam(name = "ids", value = "应收账单ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "life:feBill:remove")
    @Log(title = "应收账单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(feBillService.deleteFeBillByIds(ids));
    }
}
